﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GameSPK.Model;
using System.Windows.Controls;
using GameSPK.Helpers;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameSPK.DAO;
using System.Windows;
using System.Windows.Media.Animation;

namespace GameSPK.Start
{
    class frmSection5Start: frmGenerel
    {
        private static frmSection5Start instance;
        private static object syncLock = new object();
        int goicauhoi;
        public static frmSection5Start getInstance(int goi)
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new frmSection5Start(goi);
                        
                    }
                }
            }

            return instance;
        }
        private frmSection5Start(int goi)
            : base()
        {
            goicauhoi = goi;
            this.Closed += new EventHandler(frmSection5Start_Closed);
            this.Loaded += new System.Windows.RoutedEventHandler(frmSection5Start_Loaded);
            AddHandler(Keyboard.KeyDownEvent, (System.Windows.Input.KeyEventHandler)frmSection5Start_KeyDown);
            
        }
        string filename;
        LabelLayout lblLayoutClock;
        int timetick;
        
        CauHoi cauhoi;
        Label lblContent;
        ProgressBar probStatus;
        TextBlock tbcontent;
        void Generate()
        {
            Button btnHome;
            
            Control cbtnHome;
            btnHome = new Button();
            cbtnHome = (Control)btnHome;
            LabelLayout lableXML;
            lableXML = Helper.getButtonFromXML(filename, "HomeButton");
            AssignControl.assignControlLayout(ref cbtnHome, lableXML, "btnHome");
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getHomeButtonUrl(), UriKind.RelativeOrAbsolute));
            btnHome.Background = myBrush;
            btnHome.Click += new System.Windows.RoutedEventHandler(btnHome_Click);
            gridMain.Children.Add(btnHome);

            //content
            lblContent = new Label();
            lblContent.MouseDoubleClick += new MouseButtonEventHandler(lblContent_MouseDoubleClick);
            Control clblContent = (Control)lblContent;
            lableXML = Helper.getLabelFromXML(filename, "Content");
            AssignControl.assignControlLayout(ref clblContent, lableXML, "lblContent");
            lblContent.FontSize = lableXML.FontSize;
            lblContent.FontWeight = System.Windows.FontWeights.Bold;
            lblContent.Foreground = lableXML.Foreground;
            lblContent.Background = lableXML.Background;
            tbcontent = new TextBlock();
            tbcontent.TextWrapping = TextWrapping.Wrap;
            tbcontent.FontSize = lableXML.FontSize;
            tbcontent.Text = cauhoi.CauHoi1.Trim();
            lblContent.Content = tbcontent;

            gridMain.Children.Add(lblContent);
            //clock
            //<ProgressBar Height="28" HorizontalAlignment="Left"
            //Margin="139,95,0,0" Name="progressBar1"
            //VerticalAlignment="Top" Width="238" />
            probStatus = new ProgressBar();
            Control clblClock = (Control)probStatus;
            lblLayoutClock = Helper.getStatusFromXML(filename, "Status");
            AssignControl.assignControlLayout(ref clblClock, lblLayoutClock, "probStatus");
            //probStatus.Style = (Style)this.FindResource("PulseLabel");
            probStatus.IsIndeterminate = false;
            probStatus.Orientation = Orientation.Horizontal;
            timetick = lblLayoutClock.iTime1;
            probStatus.Maximum = 100;
            probStatus.Minimum = 0;
            probStatus.Value = 100;
            probStatus.Background = lblLayoutClock.Background;
            probStatus.Foreground = lblLayoutClock.Foreground;
            
            timeinit = timetick;
            timerClock.Enabled = true;
            timerClock.Start();
            gridMain.Children.Add(probStatus);



        }

        void lblContent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            tbcontent.Text = cauhoi.CauHoi1;
        }
        int timeinit;
        void btnHome_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Close();
        }
        void frmSection5Start_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            filename = "section" + 5 + "childlayout.xml";
            timerClock = new System.Windows.Forms.Timer();
            timerClock.Tick += new EventHandler(timerClock_Tick);
            timerClock.Interval = 1000;
            cauhoi = CauHoiDAO.getCauHoiTrinhChieu(MainWindow.getCuocThiChon(), 5, 1);
            if (cauhoi == null)
                return;
            timerClock.Enabled = false;
            Generate();
        }
        void timerClock_Tick(object sender, EventArgs e)
        {
            timetick--;
            int status;
            if (timetick >= 0)
            {
                try
                {
                    status = (int)((1.0 * timetick / (timeinit * 1.0)) * 100.0);
                    probStatus.Value = status;
                }
                catch
                {
                }
            }
            else
            {
                probStatus.Value = 0;
                tbcontent.Text = "";
                timerClock.Enabled = false;
            }
        }
        void frmSection5Start_Closed(object sender, EventArgs e)
        {
            instance = null;
        }
        void frmSection5Start_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }
        System.Windows.Forms.Timer timerClock;
    }
}
